﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections;
using System.Xml;
using System.Diagnostics;
using TomanuExtensions;
using TomanuExtensions.Utils;

namespace AmiEmuLib.Configuration.FloppySoundConfig
{
    /// <summary>
    /// Group of sounds used to simulate floppy sounds. Not all sounds must be provided.
    /// </summary>
    [DebuggerDisplay("Count = {Count}")]
    public class FloppySounds : XmlBase
    {
        /// <summary>
        /// List of all sounds.
        /// </summary>
        private FloppySound[] m_sounds;

        public readonly FloppySound Click = new FloppySound("Click");
        public readonly FloppySound Snatch = new FloppySound("Snatch");
        public readonly FloppySound Spin = new FloppySound("Spin");
        public readonly FloppySound SpinNoDisk = new FloppySound("SpinNoDisk");
        public readonly FloppySound Startup = new FloppySound("Startup");

        /// <summary>
        /// Floppy sounds name.
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// Constructor.
        /// </summary>
        internal FloppySounds(XmlReader a_reader)
        {
            m_sounds = new[] { Click, Snatch, Spin, SpinNoDisk, Startup };
            ReadXml(a_reader);
        }

        public override string ToString()
        {
            return Name;
        }

        /// <summary>
        /// List of all sounds.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<FloppySound> Sounds
        {
            get
            {
                return m_sounds;
            }
        }

        protected override void ReadXml(XmlReader a_reader)
        {
            Name = a_reader.GetAttribute("Name");

            a_reader.ReadStartElement(GetType().Name);

            while (a_reader.IsStartElement())
            {
                FloppySound fs = new FloppySound(a_reader);
                m_sounds.First(el => el.Name == fs.Name).Path = fs.Path;
            }

            a_reader.ReadEndElement();
        }

        public override void WriteXml(XmlWriter a_writer)
        {
            a_writer.WriteStartElement(GetType().Name);

            a_writer.WriteAttribute("Name", Name);

            foreach (var fs in m_sounds.Where(el => el.Path != ""))
                fs.WriteXml(a_writer);

            a_writer.WriteEndElement();
        }
    }
}
